package com.hyc.myvideo.ui.activity.my;

import android.os.CountDownTimer;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.acmenxd.toaster.Toaster;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.hyc.myvideo.R;
import com.hyc.myvideo.data.api.constants.Constants;
import com.hyc.myvideo.data.api.model.BaseResult;
import com.hyc.myvideo.data.api.model.Code;
import com.hyc.myvideo.ui.base.BaseFActivity;
import com.hyc.myvideo.widget.ClearEditText;
import com.hyc.utils.StringUtils;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 忘记密码
 * @author gyb
 * @version 1.0
 * @created 2017/07/17
 */
public class ForgetActivity extends BaseFActivity {
    /**账号/手机号*/
    @BindView(R.id.ed_account_id)
    ClearEditText edit_Phone;
    /**短信验证码*/
    @BindView(R.id.ed_verification_code_id)
    ClearEditText edit_Code;
    /**倒计时显示*/
    @BindView(R.id.fb_verification_id)
    TextView setTextViewVerification;
    /**获取输入的密码1*/
    @BindView(R.id.ed_account_password1_id)
    ClearEditText editTextPassword1;
    /**获取输入的密码2*/
    @BindView(R.id.ed_account_password2_id)
    ClearEditText editTextPassword2;
    /**提交按钮*/
    @BindView(R.id.f_bu_id)
    Button buttonSubmit;
    /**短信对象*/
    private  Code code = null;
    private TimeCount timeCount = new TimeCount(60000, 1000);
    @Override
    public int getContentViewId() {
        return R.layout.activity_forget;
    }

    @Override
    protected void initView() {
        setLeftBtnClick(true,getStr(R.string.home_head_pw));
        edit_Phone.addTextChangedListener(textWatcher);
        editTextPassword2.addTextChangedListener(textWatcherP);
        setTextViewVerification(false,"获取验证码");
        setButtonSubmit(false);
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void oNext(String r, String method) {
        BaseResult<Code> baseResult = JSONObject.parseObject(r, new TypeReference<BaseResult<Code>>(){});
        /**获取短信返回*/
        if (method.equalsIgnoreCase(Constants.API_SMS)) {
            if(baseResult.isCode()){
                this.code = baseResult.getResult();
            }
        }
        /**修改*/
        if (method.equalsIgnoreCase(Constants.API_USER_PASSWPRD)) {
            if (baseResult.isCode()) {
                this.finish();
            }
            Toaster.show(baseResult.getMsg());
        }
    }

    @Override
    protected void oError() {

    }


    @OnClick({R.id.fb_verification_id,R.id.f_bu_id})
    public void onClicks(View view) {
        switch (view.getId()){
            /**获取验证码*/
            case R.id.fb_verification_id:
                timeCount.start();//计时开始
                api.requestPhoneCode(getBEDstr(edit_Phone));
                break;
            /**确定修改密码*/
            case R.id.f_bu_id:
                if(Verification()){
                    api.updataPassword(getBEDstr(edit_Phone), getBEDstr(edit_Code),getBEDstr(editTextPassword1),getBEDstr(editTextPassword2));
                }
                break;
        }
    }

    /**
     * 定义一个倒计时的内部类
     */
    class TimeCount extends CountDownTimer {
        public TimeCount(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }
        @Override
        public void onTick(long millisUntilFinished) {
            /**按钮不可点击*/
            long length = millisUntilFinished / 1000;
            setTextViewVerification(false,length+" S");
        }
        @Override
        public void onFinish() {
            setTextViewVerification(true,"重新发送");
        }
    }
    /***
     * 设置验证码状态值
     * @param isClickable
     * @param str
     */
    private void setTextViewVerification(boolean isClickable, String str){
        if(setTextViewVerification != null){
            setTextViewVerification.setText(str);
            setTextViewVerification.setClickable(isClickable);
            if(isClickable){
                setTextViewVerification.setBackgroundResource(R.drawable.button_shape_5);
            }else{
                setTextViewVerification.setBackgroundResource(R.drawable.button_shape_5_1);
            }
        }
    }

    /***
     * 设置提交按钮状态
     * @param isClickable
     */
    private void setButtonSubmit(boolean isClickable){
        buttonSubmit.setClickable(isClickable);
        if(isClickable){
            buttonSubmit.setBackgroundResource(R.drawable.button_shape_35);
        }else{
            buttonSubmit.setBackgroundResource(R.drawable.button_shape_35_1);
        }
    }



    /***
     * 监听手机号输入框
     */
    TextWatcher textWatcher = new TextWatcher() {
        @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }
        @Override public void onTextChanged(CharSequence s, int start, int before, int count) {
        }
        @Override public void afterTextChanged(Editable s) {
            int num = s.toString().length();
            if(num >= 11 ){
                setTextViewVerification(true,"获取验证码");
            }else {
                setTextViewVerification(false,"获取验证码");
            }
            timeCount.cancel();//关闭
        }
    };

    /***
     * 监听确认密码输入框
     */
    TextWatcher textWatcherP = new TextWatcher() {
        @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }
        @Override public void onTextChanged(CharSequence s, int start, int before, int count) {
        }
        @Override public void afterTextChanged(Editable s) {
            int num = s.toString().length();
            if(num == getBEDstr(editTextPassword1).length()){
                setButtonSubmit(true);
            }else {
                setButtonSubmit(false);
            }
        }
    };

    /***
     * 验证输入两次密码是否相同
     * @return
     */
    private boolean Verification(){
        boolean isb = false;
        if(StringUtils.isEmpty(getBEDstr(edit_Phone))){
            Toaster.show("账号不能未空!");
            return isb;
        }
        if(StringUtils.isEmpty(getBEDstr(edit_Code))){
            Toaster.show("验证码不能未空!");
            return isb;
        }
        if(!code.getSms_code().equalsIgnoreCase(getBEDstr(edit_Code))){
            Toaster.show("验证码错误!");
            return isb;
        }
        if(!getBEDstr(editTextPassword1).equalsIgnoreCase(getBEDstr(editTextPassword2))){
            Toaster.show("两次输入密码不一致!");
            return isb;
        }
        isb = true;
      return isb;
    }

}
